package com.cym.controller.adminPage;

import cn.hutool.core.util.StrUtil;
import com.cym.consts.NginxConst;
import com.cym.ext.MonitorInfo;
import com.cym.ext.NetworkInfo;
import com.cym.service.MonitorService;
import com.cym.service.SettingService;
import com.cym.utils.BaseController;
import com.cym.utils.JsonResult;
import com.cym.utils.NetWorkUtil;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.core.handle.ModelAndView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Mapping("/adminPage/monitor")
@Controller
public class MonitorController extends BaseController {
    @Inject
    MonitorService monitorService;
    @Inject
    SettingService settingService;
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Mapping
    public ModelAndView index(ModelAndView modelAndView) {

        modelAndView.put("list", monitorService.getDiskInfo());

        String nginxPath = settingService.get("nginxPath");
        String nginxExe = settingService.get(NginxConst.NGINX_EXE);
        String nginxDir = settingService.get(NginxConst.NGINX_DIR);

        modelAndView.put(NginxConst.NGINX_DIR, nginxDir);
        modelAndView.put(NginxConst.NGINX_EXE, nginxExe);
        modelAndView.put("nginxPath", nginxPath);

        Boolean isInit = StrUtil.isNotEmpty(nginxExe);
        modelAndView.put("isInit", isInit.toString());

        modelAndView.view("/adminPage/monitor/index.html");
        return modelAndView;
    }

    @Mapping("check")
    public JsonResult check() {

        MonitorInfo monitorInfo = monitorService.getMonitorInfoOshi();

        return renderSuccess(monitorInfo);
    }

    @Mapping("network")
    public JsonResult network() {
        NetworkInfo networkInfo = NetWorkUtil.getNetworkDownUp();
        // System.err.println(JSONUtil.toJsonStr(networkInfo));
        return renderSuccess(networkInfo);
    }

    @Mapping("addNginxGiudeOver")
    public JsonResult addNginxGiudeOver(String nginxDir, String nginxExe, String nginxHtmlDir) {

        settingService.set(NginxConst.NGINX_DIR, nginxDir);
        settingService.set(NginxConst.NGINX_EXE, nginxExe);
        settingService.set(NginxConst.NGINX_HTML_DIR, nginxHtmlDir);
        return renderSuccess();
    }

}
